﻿using Idenmpotency;

namespace Microsoft.AspNetCore.Idenmpotency.AspNetCore.Impl
{
    /// <summary>
    /// Asp.net core 幂等性请求适配（适配器模式）：用于将IIdenmptencyRequestHandler转换成AspIdenmptencyRequestHandler
    /// </summary>
    internal class AspIdenmpotencyRequestHandler : IIdenmptencyRequestHandler
    {
        private IAspIdenmptencyRequestHandler _aspHandler;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="aspHandler"></param>
        public AspIdenmpotencyRequestHandler(IAspIdenmptencyRequestHandler aspHandler)
        {
            _aspHandler = aspHandler;
        }

        /// <summary>
        /// 首次请求处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task FirstRequestHandleAsync(IIdenmpotencyRequest request, CancellationToken token = default)
        {
           await _aspHandler.FirstRequestHandleAsync(request as IAspIdenmpotencyRequest);
        }

        /// <summary>
        /// 前置检查
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>

        public async Task<bool> PreCheckHandleAsync(IIdenmpotencyRequest request, CancellationToken token = default)
        {
            return await _aspHandler.PreCheckHandlerAsync(request as IAspIdenmpotencyRequest);
        }

        /// <summary>
        /// 重复的请求处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task ReiteratedRequestHandleAsync(IIdenmpotencyRequest request, CancellationToken token = default)
        {
            await _aspHandler.ReiteratedRequestHandleAsync(request as IAspIdenmpotencyRequest);
        }
    }
}
